import axios from '@/libs/api.request'

export default {
  storeName: 'jobStore',
  namespaced: true,
  state: {

  },
  mutations: {

  },
  actions: {
    getValidateRule () {
      return {
        scheduleName: [
          { required: true, message: '调度器名称不能为空', trigger: 'blur' }
        ],
        jobName: [
          { required: true, message: '任务名称不能为空', trigger: 'blur' }
        ],
        jobGroup: [
          { required: true, message: '任务分组不能为空', trigger: 'blur' }
        ],
        jobProviderBeanName: [
          { required: true, message: '任务执行Bean名称不能为空', trigger: 'blur' }
        ],
        jobType: [
          { required: true, message: '计划类型不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, type: 'date', message: '开始时间不能为空', trigger: 'blur' }
        ]
      }
    },
    loadList ({ state, commit }, { pageIndex, pageSize }) {
      return axios.request({
        url: '/WxJob/list',
        params: {
          pageIndex,
          pageSize
        },
        method: 'get'
      })
    },
    save ({ state, commit }, vo) {
      const requestUrl = (vo.jobId && vo.jobId !== '') ? '/WxJob/update' : '/WxJob/add'
      return axios.request({
        url: requestUrl,
        method: 'post',
        data: vo
      })
    },
    delete ({ state, commit }, { jobId }) {
      return axios.request({
        url: '/WxJob/delete',
        method: 'post',
        data: {id: jobId},
        transformRequest: [function (data) {
          // Do whatever you want to transform the data
          let ret = ''
          for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
          }
          return ret
        }],
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })
    },
    stop ({ state, commit }, { jobId }) {
      return axios.request({
        url: '/WxJob/stop',
        method: 'post',
        data: {id: jobId},
        transformRequest: [function (data) {
          // Do whatever you want to transform the data
          let ret = ''
          for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
          }
          return ret
        }],
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })
    },
    restart ({ state, commit }, { jobId }) {
      return axios.request({
        url: '/WxJob/restart',
        method: 'post',
        data: {id: jobId},
        transformRequest: [function (data) {
          // Do whatever you want to transform the data
          let ret = ''
          for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
          }
          return ret
        }],
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })
    },
    pause ({ state, commit }, { jobId }) {
      return axios.request({
        url: '/WxJob/pause',
        method: 'post',
        data: {id: jobId},
        transformRequest: [function (data) {
          // Do whatever you want to transform the data
          let ret = ''
          for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
          }
          return ret
        }],
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })
    },
    resume ({ state, commit }, { jobId }) {
      return axios.request({
        url: '/WxJob/resume',
        method: 'post',
        data: {id: jobId},
        transformRequest: [function (data) {
          // Do whatever you want to transform the data
          let ret = ''
          for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
          }
          return ret
        }],
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })
    }
  }
}
